﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class RNhanVienBO : Csla.BusinessBase<RNhanVienBO>
    {
        #region Business Properties

        private int? _NhanVienID = null;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private string _MaNhanVien = null;
        public string MaNhanVien
        {
            get { return this._MaNhanVien; }
            set { this._MaNhanVien = value; PropertyHasChanged("MaNhanVien"); }
        }

        private byte[] _Anh = null;
        public byte[] Anh
        {
            get { return this._Anh; }
            set { this._Anh = value; PropertyHasChanged("Anh"); }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private string _GioiTinh = null;
        public string GioiTinh
        {
            get { return this._GioiTinh; }
            set { this._GioiTinh = value; }
        }

        private DateTime? _NgaySinh = null;
        public DateTime? NgaySinh
        {
            get { return this._NgaySinh; }
            set { this._NgaySinh = value; PropertyHasChanged("NgaySinh"); }
        }

        private string _NoiSinh = null;
        public string NoiSinh
        {
            get { return this._NoiSinh; }
            set { this._NoiSinh = value; PropertyHasChanged("NoiSinh"); }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
        }

        private string _NguyenQuan = null;
        public string NguyenQuan
        {
            get { return this._NguyenQuan; }
            set { this._NguyenQuan = value; PropertyHasChanged("NguyenQuan"); }
        }

        private string _SoCMND = null;
        public string SoCMND
        {
            get { return this._SoCMND; }
            set { this._SoCMND = value; PropertyHasChanged("SoCMND"); }
        }

        private DateTime? _NgayCapCMND = null;
        public DateTime? NgayCapCMND
        {
            get { return this._NgayCapCMND; }
            set { this._NgayCapCMND = value; PropertyHasChanged("NgayCapCMND"); }
        }

        private string _NoiCapCMND = null;
        public string NoiCapCMND
        {
            get { return this._NoiCapCMND; }
            set { this._NoiCapCMND = value; PropertyHasChanged("NoiCapCMND"); }
        }

        private string _SoHoChieu = null;
        public string SoHoChieu
        {
            get { return this._SoHoChieu; }
            set { this._SoHoChieu = value; PropertyHasChanged("SoHoChieu"); }
        }

        private DateTime? _NgayCapHoChieu = null;
        public DateTime? NgayCapHoChieu
        {
            get { return this._NgayCapHoChieu; }
            set { this._NgayCapHoChieu = value; PropertyHasChanged("NgayCapHoChieu"); }
        }

        private DateTime? _NgayHetHanHoChieu = null;
        public DateTime? NgayHetHanHoChieu
        {
            get { return this._NgayHetHanHoChieu; }
            set { this._NgayHetHanHoChieu = value; PropertyHasChanged("NgayHetHanHoChieu"); }
        }

        private string _Email = null;
        public string Email
        {
            get { return this._Email; }
            set { this._Email = value; PropertyHasChanged("Email"); }
        }

        private string _DienThoai = null;
        public string DienThoai
        {
            get { return this._DienThoai; }
            set { this._DienThoai = value; PropertyHasChanged("DienThoai"); }
        }

        private String _DanTocID = null;
        public String DanTocID
        {
            get { return this._DanTocID; }
            set { this._DanTocID = value; PropertyHasChanged("DanTocID"); }
        }

        private String _TonGiaoID = null;
        public String TonGiaoID
        {
            get { return this._TonGiaoID; }
            set { this._TonGiaoID = value; PropertyHasChanged("TonGiaoID"); }
        }

        private String _QuocTichID = null;
        public String QuocTichID
        {
            get { return this._QuocTichID; }
            set { this._QuocTichID = value; PropertyHasChanged("QuocTichID"); }
        }

        private String _TrinhDoHocVanID = null;
        public String TrinhDoHocVanID
        {
            get { return this._TrinhDoHocVanID; }
            set { this._TrinhDoHocVanID = value; PropertyHasChanged("TrinhDoHocVanID"); }
        }

        private String _TrinhDoNgoaiNguID = null;
        public String TrinhDoNgoaiNguID
        {
            get { return this._TrinhDoNgoaiNguID; }
            set { this._TrinhDoNgoaiNguID = value; PropertyHasChanged("TrinhDoNgoaiNguID"); }
        }

        private String _TrinhDoTinHocID = null;
        public String TrinhDoTinHocID
        {
            get { return this._TrinhDoTinHocID; }
            set { this._TrinhDoTinHocID = value; PropertyHasChanged("TrinhDoTinHocID"); }
        }

        private String _BangCapID = null;
        public String BangCapID
        {
            get { return this._BangCapID; }
            set { this._BangCapID = value; PropertyHasChanged("BangCapID"); }
        }

        private String _ChuyenMonID = null;
        public String ChuyenMonID
        {
            get { return this._ChuyenMonID; }
            set { this._ChuyenMonID = value; PropertyHasChanged("ChuyenMonID"); }
        }

        private string _NoiDaoTao = null;
        public string NoiDaoTao
        {
            get { return this._NoiDaoTao; }
            set { this._NoiDaoTao = value; PropertyHasChanged("NoiDaoTao"); }
        }

        private String _PhongBanID = null;
        public String PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; PropertyHasChanged("PhongBanID"); }
        }

        private String _ChucVuID = null;
        public String ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; PropertyHasChanged("ChucVuID"); }
        }

        private String _TinhTrangCongViecID = null;
        public String TinhTrangCongViecID
        {
            get { return this._TinhTrangCongViecID; }
            set { this._TinhTrangCongViecID = value; PropertyHasChanged("TinhTrangCongViecID"); }
        }

        private DateTime? _NgayBatDauLamViec = null;
        public DateTime? NgayBatDauLamViec
        {
            get { return this._NgayBatDauLamViec; }
            set { this._NgayBatDauLamViec = value; PropertyHasChanged("NgayBatDauLamViec"); }
        }

        private DateTime? _NgayKetThucLamViec = null;
        public DateTime? NgayKetThucLamViec
        {
            get { return this._NgayKetThucLamViec; }
            set { this._NgayKetThucLamViec = value; PropertyHasChanged("NgayKetThucLamViec"); }
        }

        private string _MaHopDongLaoDong = null;
        public string MaHopDongLaoDong
        {
            get { return this._MaHopDongLaoDong; }
            set { this._MaHopDongLaoDong = value; PropertyHasChanged("MaHopDongLaoDong"); }
        }

        private String _LoaiHopDongLaoDongID = null;
        public String LoaiHopDongLaoDongID
        {
            get { return this._LoaiHopDongLaoDongID; }
            set { this._LoaiHopDongLaoDongID = value; PropertyHasChanged("LoaiHopDongLaoDongID"); }
        }

        private DateTime? _NgayKyHopDongLaoDong = null;
        public DateTime? NgayKyHopDongLaoDong
        {
            get { return this._NgayKyHopDongLaoDong; }
            set { this._NgayKyHopDongLaoDong = value; PropertyHasChanged("NgayKyHopDongLaoDong"); }
        }

        private DateTime? _NgayKetThucHopDongLaoDong = null;
        public DateTime? NgayKetThucHopDongLaoDong
        {
            get { return this._NgayKetThucHopDongLaoDong; }
            set { this._NgayKetThucHopDongLaoDong = value; PropertyHasChanged("NgayKetThucHopDongLaoDong"); }
        }

        private String _NgachLuongID = null;
        public String NgachLuongID
        {
            get { return this._NgachLuongID; }
            set { this._NgachLuongID = value; PropertyHasChanged("NgachLuongID"); }
        }

        private String _BacLuongID = null;
        public String BacLuongID
        {
            get { return this._BacLuongID; }
            set { this._BacLuongID = value; PropertyHasChanged("BacLuongID"); }
        }

        private DateTime? _NgayApDungLuong = null;
        public DateTime? NgayApDungLuong
        {
            get { return this._NgayApDungLuong; }
            set { this._NgayApDungLuong = value; PropertyHasChanged("NgayApDungLuong"); }
        }

        private double? _HeSoLuong = 0;
        public double? HeSoLuong
        {
            get { return this._HeSoLuong; }
            set { this._HeSoLuong = value; PropertyHasChanged("HeSoLuong"); }
        }

        private double? _LuongCoBan = 0;
        public double? LuongCoBan
        {
            get { return this._LuongCoBan; }
            set { this._LuongCoBan = value; PropertyHasChanged("LuongCoBan"); }
        }

        private double? _ThanhTien = 0;
        public double? ThanhTien
        {
            get { return this._ThanhTien; }
            set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
        }

        private double? _LuongBHXH = 0;
        public double? LuongBHXH
        {
            get { return this._LuongBHXH; }
            set { this._LuongBHXH = value; PropertyHasChanged("LuongBHXH"); }
        }

        private bool? _BHXH = false;
        public bool? BHXH
        {
            get { return this._BHXH; }
            set { this._BHXH = value; PropertyHasChanged("BHXH"); }
        }

        private bool? _BHYT = false;
        public bool? BHYT
        {
            get { return this._BHYT; }
            set { this._BHYT = value; PropertyHasChanged("BHYT"); }
        }

        private bool? _BHTN = false;
        public bool? BHTN
        {
            get { return this._BHTN; }
            set { this._BHTN = value; PropertyHasChanged("BHTN"); }
        }

        private double? _BHXH_PCCV = 0;
        public double? BHXH_PCCV
        {
            get { return this._BHXH_PCCV; }
            set { this._BHXH_PCCV = value; PropertyHasChanged("BHXH_PCCV"); }
        }

        private double? _BHXH_TNN = 0;
        public double? BHXH_TNN
        {
            get { return this._BHXH_TNN; }
            set { this._BHXH_TNN = value; PropertyHasChanged("BHXH_TNN"); }
        }

        private double? _BHXH_TNVK = 0;
        public double? BHXH_TNVK
        {
            get { return this._BHXH_TNVK; }
            set { this._BHXH_TNVK = value; PropertyHasChanged("BHXH_TNVK"); }
        }

        private double? _BHXH_TNK = 0;
        public double? BHXH_TNK
        {
            get { return this._BHXH_TNK; }
            set { this._BHXH_TNK = value; PropertyHasChanged("BHXH_TNK"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _NhanVienID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newNhanVienID = -1;
        private RNhanVienBO()
        {
            _NhanVienID = newNhanVienID--;
        }

        public RNhanVienBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public RNhanVienBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static RNhanVienBO NewRootRNhanVienBO()
        {
            return DataPortal.Create<RNhanVienBO>(new RootCriteria());
        }

        internal static RNhanVienBO NewChildRNhanVienBO()
        {
            return DataPortal.Create<RNhanVienBO>(new ChildCriteria());
        }

        public static RNhanVienBO GetRootRNhanVienBO(int _NhanVienID)
        {
            return DataPortal.Fetch<RNhanVienBO>(new RootCriteria(_NhanVienID));
        }

        public static RNhanVienBO GetRootRNhanVienBOByWhere(string where)
        {
            return DataPortal.Fetch<RNhanVienBO>(where);
        }

        internal static RNhanVienBO GetChildRNhanVienBO(SqlDataReader dataReader)
        {
            return new RNhanVienBO(dataReader);
        }

        public static void DeleteRNhanVienBO(int _NhanVienID)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID));
        }

        public static void DeleteRNhanVienBO(int _NhanVienID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_NhanVienID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public RootCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public RootCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int NhanVienID;
            public bool directDelete = false;
            public ChildCriteria(int _NhanVienID)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = false;
            }

            public ChildCriteria(int _NhanVienID, bool directDelete)
            {
                this.NhanVienID = _NhanVienID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
        }

        private void This_Fetch(string where)
        {
        }

        private void Fetch(SqlDataReader dataReader)
        {
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        public void CopyFromNhanVienBO(NhanVienBO nvBO)
        {
            this.NhanVienID = nvBO.NhanVienID;
            MaNhanVien = nvBO.MaNhanVien;
            Anh = nvBO.Anh;
            HoTen = nvBO.Ho + " " + nvBO.Ten;
            GioiTinh = (StaticUtil.GetBool(nvBO.Nu) ? "Nữ" : "Nam");
            NgaySinh = nvBO.NgaySinh;
            NoiSinh = nvBO.NoiSinh;
            DiaChi = nvBO.DiaChi;
            NguyenQuan = nvBO.NguyenQuan;
            SoCMND = nvBO.SoCMND;
            NgayCapCMND = nvBO.NgayCapCMND;
            NoiCapCMND = nvBO.NoiCapCMND;
            SoHoChieu = nvBO.SoHoChieu;
            NgayCapHoChieu = nvBO.NgayCapHoChieu;
            NgayHetHanHoChieu = nvBO.NgayHetHanHoChieu;
            Email = nvBO.Email;
            DienThoai = nvBO.DienThoai;
            DanTocID = StaticUtil.GetInt(nvBO.DanTocID).ToString();
            TonGiaoID = StaticUtil.GetInt(nvBO.TonGiaoID).ToString();
            QuocTichID = StaticUtil.GetInt(nvBO.QuocTichID).ToString();
            TrinhDoHocVanID = StaticUtil.GetInt(nvBO.TrinhDoHocVanID).ToString();
            TrinhDoNgoaiNguID = StaticUtil.GetInt(nvBO.TrinhDoNgoaiNguID).ToString();
            TrinhDoTinHocID = StaticUtil.GetInt(nvBO.TrinhDoTinHocID).ToString();
            BangCapID = StaticUtil.GetInt(nvBO.BangCapID).ToString();
            ChuyenMonID = StaticUtil.GetInt(nvBO.ChuyenMonID).ToString();
            NoiDaoTao = nvBO.NoiDaoTao;
            PhongBanID = StaticUtil.GetInt(nvBO.PhongBanID).ToString();
            ChucVuID = StaticUtil.GetInt(nvBO.ChucVuID).ToString();
            TinhTrangCongViecID = StaticUtil.GetInt(nvBO.TinhTrangCongViecID).ToString();
            NgayBatDauLamViec = nvBO.NgayBatDauLamViec;
            NgayKetThucLamViec = nvBO.NgayKetThucLamViec;
            MaHopDongLaoDong = nvBO.MaHopDongLaoDong;
            LoaiHopDongLaoDongID = StaticUtil.GetInt(nvBO.LoaiHopDongLaoDongID).ToString();
            NgayKyHopDongLaoDong = nvBO.NgayKyHopDongLaoDong;
            NgayKetThucHopDongLaoDong = nvBO.NgayKetThucHopDongLaoDong;
            NgachLuongID = StaticUtil.GetInt(nvBO.NgachLuongID).ToString();
            BacLuongID = StaticUtil.GetInt(nvBO.BacLuongID).ToString();
            NgayApDungLuong = nvBO.NgayApDungLuong;
            HeSoLuong = nvBO.HeSoLuong;
            LuongCoBan = nvBO.LuongCoBan;
            ThanhTien = nvBO.ThanhTien;
            LuongBHXH = nvBO.LuongBHXH;
            BHXH = nvBO.BHXH;
            BHYT = nvBO.BHYT;
            BHTN = nvBO.BHTN;
            BHXH_PCCV = nvBO.BHXH_PCCV;
            BHXH_TNN = nvBO.BHXH_TNN;
            BHXH_TNVK = nvBO.BHXH_TNVK;
            BHXH_TNK = nvBO.BHXH_TNK;
            GhiChu = nvBO.GhiChu;
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_NhanVienID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
